package com.himiotech.core.project.helper.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.map.MapUtil;
import com.himiotech.core.framework.helper.model.XmSelectTree;
import com.himiotech.core.project.model.entity.Menu;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 权限数据处理
 *
 * @author ruoyi
 * @author wuwenbin
 */
public class TreeUtils {

    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list     分类表
     * @param parentId 传入的父节点ID
     * @return String
     */
    public static List<Menu> getChildPerms(List<Menu> list, int parentId) {
        List<Menu> returnList = new ArrayList<>();
        for (Menu t : list) {
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId() == parentId) {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    public static List<Tree<String>> getMenuXmSelectTree(List<Menu> menus, boolean permsFlag) {
        List<TreeNode<String>> menuTrees = menus.stream()
                .map(menu -> {
                    TreeNode<String> treeNode = new TreeNode<>(menu.getMenuId().toString(), menu.getParentId().toString(), transMenuName(menu, permsFlag), menu.getOrderNum());
                    treeNode.setExtra(MapUtil.of("menu", menu));
                    return treeNode;
                })
                .collect(Collectors.toList());
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("value");

        return TreeUtil.build(menuTrees, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setName(treeNode.getName());
                    tree.putExtra("perms", BeanUtil.toBean(treeNode.getExtra().get("menu"), Menu.class).getPerms());
                });
    }

    public static String transMenuName(Menu menu, boolean permsFlag) {
        StringBuilder sb = new StringBuilder();
        sb.append(menu.getMenuName());
        if (permsFlag) {
            sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;").append(menu.getPerms()).append("</font>");
        }
        return sb.toString();
    }

    /**
     * 递归添加子菜单
     *
     * @param parent
     * @param roleMenuList
     */
    public static void addXmSelectTreeChildrenByParent(XmSelectTree xmSelectTree, Tree<String> parent, List<String> roleMenuList) {
        List<Tree<String>> children = parent.getChildren();
        boolean isCheck = roleMenuList != null;
        if (CollectionUtil.isNotEmpty(children)) {
            List<XmSelectTree> result = new ArrayList<>(children.size());
            for (Tree<String> menu : children) {
                XmSelectTree tree = new XmSelectTree();
                tree.setValue(menu.getId());
                tree.setName(menu.getName().toString());
                if (isCheck) {
                    tree.setSelected(roleMenuList.contains(menu.getId() + menu.get("perms")));
                }
                result.add(tree);
                xmSelectTree.setChildren(result);
                List<Tree<String>> children1 = menu.getChildren();
                if (CollectionUtil.isNotEmpty(children1)) {
                    addXmSelectTreeChildrenByParent(tree, menu, roleMenuList);
                }
            }
        }
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private static void recursionFn(List<Menu> list, Menu t) {
        // 得到子节点列表
        List<Menu> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Menu tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private static List<Menu> getChildList(List<Menu> list, Menu t) {

        List<Menu> tlist = new ArrayList<>();
        for (Menu n : list) {
            if (n.getParentId().longValue() == t.getMenuId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private static boolean hasChild(List<Menu> list, Menu t) {
        return getChildList(list, t).size() > 0;
    }

}
